package com.bw.publish

import android.os.Build
import android.os.Environment
import androidx.annotation.RequiresApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.publish.common.ConstPublish
import com.bw.publish.utils.CallBack
import com.bw.publish.utils.OSSUtils
import java.io.File


@RequiresApi(Build.VERSION_CODES.R)
@Composable
fun PublishPage() {
    val context = LocalContext.current

    Column(
        Modifier
            .fillMaxSize()
            .background(Color(0xFFEDEDED))
    ) {
        Row(
            Modifier
                .fillMaxWidth()
                .padding(end = 10.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.End
        ) {
            Image(
                painter = painterResource(id = R.drawable.ic_caogaoxiang),
                contentDescription = ""
            )

            Text(text = "草稿箱", color = Color(0xFFA8A8A8))
        }

        Text(
            text = "hi开启创作之旅吧",
            color = Color(0xFF767676),
            fontSize = 20.sp,
            modifier = Modifier.padding(start = 5.dp)
        )

        Row(
            Modifier
                .fillMaxWidth()
                .padding(start = 5.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painter = painterResource(id = R.drawable.ic_moviesmall),
                contentDescription = ""
            )

            Text(text = "用视频分享生活", color = Color(0xFFA8A8A8))
        }

        Spacer(modifier = Modifier.height(20.dp))

        Row(
            Modifier
                .fillMaxWidth()
                .height(100.dp)
                .background(Color(0xFFA8A8A8)),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center
        ) {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier.clickable {
                    ToastUtils.showLong("开拍")
                    ARouter.getInstance().build(ConstPublish.PUBLISH_TAKE).navigation()
                }) {
                Image(
                    painter = painterResource(id = R.drawable.ic_movie),
                    contentDescription = "",
                    contentScale = ContentScale.Crop, modifier = Modifier.size(60.dp)
                )

                Text(text = "现在开拍", color = Color.White)
            }
        }

        Spacer(modifier = Modifier.height(10.dp))

        Row(
            Modifier
                .fillMaxWidth()
                .height(50.dp)
                .background(Color.White)
                .padding(start = 10.dp), verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painter = painterResource(id = R.drawable.ic_upload),
                contentDescription = "",
                modifier = Modifier.clickable {
                    val videoPath =
                        Environment.getExternalStorageDirectory().absolutePath + File.separator + ConstPublish.VIDEO_RECORDER_PATH
                    val file = File(videoPath)
                    if (!file.exists()) {
                        ToastUtils.showLong("文件不存在")
                    }
                    OSSUtils.upLoadObject(
                        ConstPublish.VIDEO_RECORDER_PATH,
                        videoPath,
                        object : CallBack {
                            override fun <T> onSuccess(t: T) {
                                ToastUtils.showLong("上传成功")
                            }

                            override fun onFailed(throwable: Throwable) {
                                ToastUtils.showLong("上传失败")
                            }

                        })
                })
            Spacer(modifier = Modifier.width(10.dp))
            Text(text = "上传", color = Color(0xFFA8A8A8))
        }

        Spacer(modifier = Modifier.height(10.dp))

        Row(
            Modifier
                .fillMaxWidth()
                .height(60.dp)
                .background(Color.White),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center
        ) {

            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_template),
                    contentDescription = ""
                )
                Text(text = "视频编辑", color = Color(0xFFA8A8A8))
            }

            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_material),
                    contentDescription = ""
                )
                Text(text = "免费素材", color = Color(0xFFA8A8A8))
            }

            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_toolbox),
                    contentDescription = ""
                )
                Text(text = "工具箱", color = Color(0xFFA8A8A8))
            }

            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_cut),
                    contentDescription = ""
                )
                Text(text = "视频剪辑", color = Color(0xFFA8A8A8))
            }
        }


    }
}

@RequiresApi(Build.VERSION_CODES.R)
@Preview(showBackground = true)
@Composable
fun show() {
    PublishPage()
}